package DFS;

import java.util.*;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-22
 * Time:10:22
 * Text：17. 电话号码的字母组合
 */
public class Text9 {
    HashMap<Character,String> map=new HashMap<>();
    List<String> t1;
    StringBuffer str;
    int num;
    void dfs(char[][] array,int pos){
        if(num == pos) {
            t1.add(str.toString());
            return;
        }
            for(int j=0;j<array[pos].length;j++){
                str.append(array[pos][j]);
                dfs(array,pos+1);
                //消除
                str.deleteCharAt(str.length()-1);
            }
    }
    public List<String> letterCombinations(String digits) {
        map.put('2',"abc");
        map.put('3',"def");
        map.put('4',"ghi");
        map.put('5',"jkl");
        map.put('6',"mno");
        map.put('7',"pqrs");
        map.put('8',"tuv");
        map.put('9',"wxyz");
        t1=new LinkedList<>();
        num=digits.length();
        str=new StringBuffer();
        if(digits.equals("")){
            return new ArrayList<>();
        }
        char[][] array=new char[digits.length()][];
        for(int i=0;i<digits.length();i++){
            char[] t=map.get(digits.charAt(i)).toCharArray();
            array[i]=t;
        }
        dfs(array,0);
        return t1;
    }

    public static void main(String[] args) {
        Text9 t1=new Text9();
        t1.letterCombinations("23");
    }
}
